package com.dongwu.security.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.dongwu.common.web.BaseController;
import com.dongwu.security.entity.Group;
import com.dongwu.security.entity.User;
import com.dongwu.security.service.IUserService;



@Controller
@RequestMapping("/security/group")
public class GroupController extends BaseController<Group, String> {

	@Autowired
	private IUserService userService;

	@Value("${dongwu.security.group.queryRole}")
	private String queryRole;

	@Value("${dongwu.security.group.addRole}")
	private String addRole;

	@Value("${dongwu.security.group.editRole}")
	private String editRole;

	@Value("${dongwu.security.group.deleteRole}")
	private String deleteRole;

	@Value("${dongwu.security.group.batchDeleteRole}")
	private String batchDeleteRole;
	
	@RequestMapping(value = "/add")
	public String add(ModelMap model) {
		
		List<User> users = userService.findAll();
		model.addAttribute("users", users);
		
		return super.add(model);

	}
	
	@RequestMapping(value = "/edit/{id}")
	public String edit(ModelMap model) {
		
		List<User> users = userService.findAll();
		model.addAttribute("users", users);
		
		return super.edit(model);

	}

	@ModelAttribute
	public void getEntity(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!String.valueOf(id).equals("-1")) {
			Group group = baseService.get(id);
			group.getRoles().clear();
			group.getUsers().clear();
//			group.getDataPermission().clear();
			model.addAttribute("entity", group);
		}
	}

	public String getQueryRole() {
		return queryRole;
	}

	public String getAddRole() {
		return addRole;
	}

	public String getEditRole() {
		return editRole;
	}

	public String getDeleteRole() {
		return deleteRole;
	}

	public String getBatchDeleteRole() {
		return batchDeleteRole;
	}
	
	
}
